package com.barbeque.mall.service.admin.goods.impl;

import com.barbeque.mall.bean.goods.*;
import com.barbeque.mall.bean.goods.bo.GoodsAllinone;
import com.barbeque.mall.bean.goods.bo.GoodsListBO;
import com.barbeque.mall.bean.goods.vo.CatAndBrandVO;
import com.barbeque.mall.bean.goods.vo.DetailGoodsVO;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.mapper.*;
import com.barbeque.mall.service.admin.goods.AdminGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Euphonium
 * @since 2022/07/18
 */
@Service
public class AdminGoodsServiceImpl implements AdminGoodsService {

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;

    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public CommonData<MarketGoods> listGoods(GoodsListBO goodsListBO) {
        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        // 获取每页大小
        Integer limit = goodsListBO.getLimit();
        if (limit != 0) {
            // 开启分页
            PageHelper.startPage(goodsListBO.getPage(), goodsListBO.getLimit());
            // 设置排序规则
            example.setOrderByClause(goodsListBO.getSort() + " " + goodsListBO.getOrder());
        }
        // 根据商品ID查询
        if (goodsListBO.getGoodsId() != null) {
            criteria.andIdEqualTo(goodsListBO.getGoodsId());
        }
        // 根据商品编号查询
        if (goodsListBO.getGoodsSn() != null) {
            criteria.andGoodsSnEqualTo(goodsListBO.getGoodsSn());
        }
        // 根据商品名称模糊查询
        if (goodsListBO.getName() != null) {
            criteria.andNameLike("%" + goodsListBO.getName() + "%");
        }
        criteria.andDeletedEqualTo(false);
        // 执行查询语句
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(example);
        // 获取PageInfo
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(marketGoods);
        CommonData data = CommonData.data(pageInfo);
        if (limit == 0) {
            data.setPage(0);
            data.setPages(0);
            data.setLimit(0);
        }
        return data;
    }

    @Override
    public CatAndBrandVO listCatAndBrand() {
        CatAndBrandVO catAndBrandVO = new CatAndBrandVO();
        List<CatAndBrandVO.BrandListBean> brandList = marketGoodsMapper.selectIdAndNameOfBrand();
        catAndBrandVO.setBrandList(brandList);

        // 找出所有分类
        List<CatAndBrandVO.CategoryListBean> categoryList = marketGoodsMapper.selectIdAndNameOfCategory();
        for (CatAndBrandVO.CategoryListBean category : categoryList) {
            Integer pid = category.getValue();
            // 找出该分类下的所有子分类
            List<CatAndBrandVO.CategoryListBean.ChildrenBean> childCategoryList = marketGoodsMapper.selectIdAndNameOfCategoryByPid(pid);
            category.setChildren(childCategoryList);
        }
        catAndBrandVO.setCategoryList(categoryList);
        return catAndBrandVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createGoods(GoodsAllinone goodsAllinone) {
        // 获得新建商品的基本信息
        MarketGoods goodsRecord = goodsAllinone.getGoods();
        // 校验当前商品名是否已存在
        MarketGoodsExample goodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
        goodsExampleCriteria.andNameEqualTo(goodsRecord.getName());
        goodsExampleCriteria.andDeletedEqualTo(false);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(goodsExample);
        if (marketGoods.size() > 0) {
            // 商品名已经存在，添加失败
            return false;
        }
        // 获取新建商品的创建时间
        Date createTime = new Date();
        // 将商品基本信息插入商品基本信息表
        goodsRecord.setAddTime(createTime);
        goodsRecord.setUpdateTime(createTime);
        marketGoodsMapper.insertSelective(goodsRecord);
        // 获取新建商品的商品ID
        Integer goodsId = goodsRecord.getId();
        // 将新增商品的参数信息插入商品参数表
        List<MarketGoodsAttribute> goodsAttributeRecords = goodsAllinone.getAttributes();
        for (MarketGoodsAttribute record : goodsAttributeRecords) {
            record.setGoodsId(goodsId);
            record.setAddTime(createTime);
            record.setUpdateTime(createTime);
            marketGoodsAttributeMapper.insertSelective(record);
        }
        // 将新增商品的库存信息插入商品货品表
        List<MarketGoodsProduct> goodsProductRecords = goodsAllinone.getProducts();
        for (MarketGoodsProduct record : goodsProductRecords) {
            record.setGoodsId(goodsId);
            record.setAddTime(createTime);
            record.setUpdateTime(createTime);
            marketGoodsProductMapper.insertSelective(record);
        }
        // 将新增商品的规格信息插入商品规格表
        List<MarketGoodsSpecification> goodsSpecificationRecords = goodsAllinone.getSpecifications();
        for (MarketGoodsSpecification record : goodsSpecificationRecords) {
            record.setGoodsId(goodsId);
            record.setAddTime(createTime);
            record.setUpdateTime(createTime);
            marketGoodsSpecificationMapper.insertSelective(record);
        }
        return true;
    }

    @Override
    public DetailGoodsVO detailGoods(Integer goodsId) {
        DetailGoodsVO detailGoodsVO = new DetailGoodsVO();
        // setGoods
        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(goodsId);
        detailGoodsVO.setGoods(marketGoods);
        // setAttributes
        List<MarketGoodsAttribute> marketGoodsAttributes = marketGoodsAttributeMapper.selectByGoodsId(goodsId);
        detailGoodsVO.setAttributes(marketGoodsAttributes);
        // setSpecifications
        List<MarketGoodsSpecification> marketGoodsSpecifications = marketGoodsSpecificationMapper.selectByGoodsId(goodsId);
        detailGoodsVO.setSpecifications(marketGoodsSpecifications);
        // setProducts
        List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByGoodsId(goodsId);
        detailGoodsVO.setProducts(marketGoodsProducts);
        // setCategoryIds
        Integer[] categoryIds = new Integer[0];
        // 商品所属类目ID默认为0
        Integer categoryId = marketGoods.getCategoryId();
        if (categoryId != 0) {
            Integer categoryPid = marketCategoryMapper.selectByPrimaryKey(categoryId).getPid();
            categoryIds = new Integer[]{categoryId, categoryPid};
        }
        detailGoodsVO.setCategoryIds(categoryIds);
        return detailGoodsVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateGoods(GoodsAllinone goodsAllinone) {
        // 获取更新时间
        Date updateTime = new Date();
        // 获取更新后的商品基本信息
        MarketGoods goodsRecord = goodsAllinone.getGoods();
        // 获取商品ID(商品ID不可更改，根据商品ID进行信息更新)
        Integer goodsId = goodsRecord.getId();
        // 更新商品基本信息表
        goodsRecord.setUpdateTime(updateTime);
        marketGoodsMapper.updateByPrimaryKey(goodsRecord);
        // 更新商品参数表
        List<MarketGoodsAttribute> goodsAttributeRecords = goodsAllinone.getAttributes();
        for (MarketGoodsAttribute record : goodsAttributeRecords) {
            record.setUpdateTime(updateTime);
            marketGoodsAttributeMapper.updateByGoodsId(record, goodsId);
        }
        // 更新商品货品表
        List<MarketGoodsProduct> goodsProductRecords = goodsAllinone.getProducts();
        for (MarketGoodsProduct record : goodsProductRecords) {
            record.setUpdateTime(updateTime);
            marketGoodsProductMapper.updateByGoodsId(record, goodsId);
        }
        // 更新商品规格表
        List<MarketGoodsSpecification> goodsSpecificationRecords = goodsAllinone.getSpecifications();
        for (MarketGoodsSpecification record : goodsSpecificationRecords) {
            record.setUpdateTime(updateTime);
            marketGoodsSpecificationMapper.updateByGoodsId(record, goodsId);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteGoods(MarketGoods marketGoods) {
        GoodsAllinone goodsAllinone = new GoodsAllinone();
        // 获取待删除商品的商品ID
        Integer goodsId = marketGoods.getId();
        // 修改商品基本信息(更新时间在调用updateGoods时修改，此处只需修改逻辑删除字段，下同)
        marketGoods.setDeleted(true);
        // 修改商品参数信息
        List<MarketGoodsAttribute> marketGoodsAttributes = marketGoodsAttributeMapper.selectByGoodsId(goodsId);
        for (MarketGoodsAttribute marketGoodsAttribute : marketGoodsAttributes) {
            marketGoodsAttribute.setDeleted(true);
        }
        // 修改商品库存信息
        List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByGoodsId(goodsId);
        for (MarketGoodsProduct marketGoodsProduct : marketGoodsProducts) {
            marketGoodsProduct.setDeleted(true);
        }
        // 修改商品规格信息
        List<MarketGoodsSpecification> marketGoodsSpecifications = marketGoodsSpecificationMapper.selectByGoodsId(goodsId);
        for (MarketGoodsSpecification marketGoodsSpecification : marketGoodsSpecifications) {
            marketGoodsSpecification.setDeleted(true);
        }
        // 将上述信息封装到GoodsAllinone中
        goodsAllinone.setGoods(marketGoods);
        goodsAllinone.setAttributes(marketGoodsAttributes);
        goodsAllinone.setProducts(marketGoodsProducts);
        goodsAllinone.setSpecifications(marketGoodsSpecifications);
        updateGoods(goodsAllinone);
    }
}
